package com.macaline.framemarker.dataBase;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.macaline.framemarker.dataBase.framework.bean.Column;
import com.macaline.framemarker.dataBase.framework.bean.Const;
import com.macaline.framemarker.dataBase.framework.bean.Table;
import com.macaline.framemarker.dataBase.framework.builder.AbstractTableBuilder;
import com.macaline.framemarker.dataBase.mysql.builder.MySqlTableBuilder;
import com.macaline.framemarker.dataBase.mysql.enums.ColumnType;
import com.macaline.framemarker.dataBase.mysql.session.MySQLSession;
import com.macaline.framemarker.dataBase.mysql.util.SqlUtil;

import java.util.LinkedList;

public class Main {


    public static void main(String[] args) throws Exception {
        LinkedList<String> bankReceiptCloumList = new LinkedList<>();
        bankReceiptCloumList.add("receipt_number"); // 回单编号
        bankReceiptCloumList.add("trade_type"); // 交易类型
        bankReceiptCloumList.add("trade_date"); // 交易日期
        bankReceiptCloumList.add("payer_account"); // 付款账号
        bankReceiptCloumList.add("payer_name"); // 付款户名
        bankReceiptCloumList.add("payer_bank"); // 付款银行
        bankReceiptCloumList.add("payee_account"); // 收款账号
        bankReceiptCloumList.add("payee_name"); // 收款户名
        bankReceiptCloumList.add("payee_bank"); // 收款银行
        bankReceiptCloumList.add("abstract"); // 摘要
        bankReceiptCloumList.add("postscript"); // 附言
        bankReceiptCloumList.add("usage"); // 用途
        bankReceiptCloumList.add("loan_mark"); // 借贷标志
        bankReceiptCloumList.add("note"); // 备注
        bankReceiptCloumList.add("business_type"); // 业务类型
        bankReceiptCloumList.add("amount_big"); // 金额大写
        bankReceiptCloumList.add("amount_small"); // 金额小写
        bankReceiptCloumList.add("bank_postscript"); // 银行附言
        bankReceiptCloumList.add("customer_postscript"); // 客户附言
        bankReceiptCloumList.add("registered_number"); // 实际记账账号
        bankReceiptCloumList.add("registered_name"); // 实际记账户名
        bankReceiptCloumList.add("currency"); // 货币种类
        bankReceiptCloumList.add("amount"); // 金额
        bankReceiptCloumList.add("remark"); // 备注/附言
        bankReceiptCloumList.add("name"); // 户名
        bankReceiptCloumList.add("account"); // 账号
        bankReceiptCloumList.add("serial_number"); // 交易流水号/记账流水号
        bankReceiptCloumList.add("trade_time"); // 交易时间
        bankReceiptCloumList.add("trade_time"); //

        AbstractTableBuilder tableBuilder = new MySqlTableBuilder()
            .tabName("ocr_bank_receipt");
        tableBuilder.column(Column.builder().name("id").type(ColumnType.CHAR.getCode()).length(36).comment("id").primaryKey(Const.IS_YES).build());
        tableBuilder.column(Column.builder().name("file_id").type(ColumnType.CHAR.getCode()).length(36).comment("文件ID").build());


        Table table = tableBuilder.comment("银行回执单OCR数据").build();



        String insertSql = SqlUtil.getInsertSql(table);
        System.out.println(insertSql);
        System.out.println(SqlUtil.getBeanCode(table));

        





//        MySQLSession ses = new MySQLSession();
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setUrl("jdbc:mysql://localhost:3306/macaline?characterEncoding=utf8&useSSL=false");
//        dataSource.setUsername("root");
//        dataSource.setPassword("123456");
//        ses.setDataSource(dataSource);

        // 悲观锁
//        lock(ses);


//
//        Table table = getTable();
//        Boolean isExistsTab = ses.isExistsTab(table);
//        System.out.println(isExistsTab);
//        ses.dropTable(table);
//        ses.createTable(table);
//        ses.executeQuery("DESC police_shunt");
    }


    public static void lock(MySQLSession ses){
        new Thread(() -> {
            try {
                while (true){
                    ses.executeByTransaction("SELECT * FROM role WHERE id = 2 FOR UPDATE;");
                }
            } catch (Exception e) {
                System.err.println(Thread.currentThread().getName() + e.getMessage());
                throw new RuntimeException(e);
            }
        }, "for update").start();

        new Thread(() -> {
            try {
                while (true) {
                    // 只有
                    Boolean execute = ses.executeByTransaction("SELECT * FROM role WHERE id = 2 FOR UPDATE;");
                }
            } catch (Exception e) {
                System.err.println(Thread.currentThread().getName() + e.getMessage());
                throw new RuntimeException(e);
            }
        }, "for update").start();

        new Thread(() -> {
            try {
                while (true) {
                    // 只有
                    Boolean execute = ses.execute("SELECT * FROM role WHERE id = 2;");
                }
            } catch (Exception e) {
                System.err.println(Thread.currentThread().getName() + e.getMessage());
                throw new RuntimeException(e);
            }
        }, "common select").start();
    }

    public static Table getTable(){
        Table table = new MySqlTableBuilder()
                .tabName("sys_user")
                .comment("用户信息表")
                .column(Column.builder().name("id").type(ColumnType.CHAR.getCode()).length(32).comment("id").primaryKey(Const.IS_YES).build())
                .column(Column.builder().name("user_name").type(ColumnType.CHAR.getCode()).length(32).comment("姓名").build())
                .column(Column.builder().name("age").type(ColumnType.INT.getCode()).length(32).comment("年龄").build())
                .build();
        return table;
    }
}
